/**
 * 
 */
package com.arsl.client.view;

import java.util.Date;

import com.arsl.client.PaperaziServiceAsync;
import com.arsl.client.data.Paper;
import com.arsl.client.presenter.CreateArPagePresenter;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CaptionPanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author NuwanSam
 * 
 */
public class CreateARPageView extends Composite {

	@UiTemplate("CreateARPage.ui.xml")
	interface CreateARPageUiBinder extends UiBinder<Widget, CreateARPageView> {
	}

	private static CreateARPageUiBinder uiBinder = GWT
			.create(CreateARPageUiBinder.class);

	private PaperaziServiceAsync rpcService;

	public void setRpcService(PaperaziServiceAsync rpcService) {
		this.rpcService = rpcService;
		pageSelector.setRpcService(rpcService);
	}

	/**
	 * Because this class has a default constructor, it can be used as a binder
	 * template. In other words, it can be used in other *.ui.xml files as
	 * follows: <ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
	 * xmlns:g="urn:import:**user's package**">
	 * <g:**UserClassName**>Hello!</g:**UserClassName> </ui:UiBinder> Note that
	 * depending on the widget that is used, it may be necessary to implement
	 * HasHTML instead of HasText.
	 */
	public CreateARPageView() {
		initWidget(uiBinder.createAndBindUi(this));
		cpPanelQr.setVisible(false);
		btnNext.setVisible(false);
	}

	@UiField
	PageSelectorWidget pageSelector;

	@UiField
	Button btnCreateARPage;

	@UiField
	Image imgQr;

	@UiField
	CaptionPanel cpPanelQr;

	@UiField
	Button btnNext;

	private CreateArPagePresenter presenter;

	@UiHandler("btnNext")
	public void onNextClick(ClickEvent e) {
		presenter.goToTagging();
	}

	@UiHandler("btnCreateARPage")
	public void onCreateArClick(ClickEvent e) {

		if (pageSelector.isValid()) {
			int paperId = pageSelector.getPaperId();
			Date date = pageSelector.getDate();
			long t = date.getTime();
			java.sql.Date dt = new java.sql.Date(t);

			presenter.createARPage(paperId, dt, pageSelector.getEditionId(),
					pageSelector.getSectionId(), pageSelector.getPageNo());
		} else {
			Window.alert("Incorrect parameters for the page selection");
		}
	}

	public void setPresenter(CreateArPagePresenter createArPagePresenter) {
		this.presenter = createArPagePresenter;
	}

	public void setImage(String string) {
		cpPanelQr.setVisible(true);
		btnNext.setVisible(true);
		imgQr.setUrl(string);
	}

	public void populatePapers() {
		pageSelector.populatePapers();
	}

}
